/*
 *   A task scheduler that implements Monte-Carlo simulation
 *
 *   Copyright (C) 2011 Systasis Computer Systems, Inc.
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *   
 *   $Id: GanttView.java 85 2011-08-08 21:09:14Z jchimene@gmail.com $
 */
package com.systasis.sked.client.view;

import java.util.Date;

import com.arcetis.gwt.jsgantt.client.Format;
import com.arcetis.gwt.jsgantt.client.GanttChartListener;
import com.arcetis.gwt.jsgantt.client.Priority;
import com.arcetis.gwt.jsgantt.client.TaskItem;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.gwtplatform.mvp.client.PopupViewImpl;
import com.systasis.sked.client.presenter.GanttPresenter;
import com.systasis.sked.client.view.GanttHandlers.GanttUiHandlers;

@Singleton
public class GanttView extends PopupViewImpl implements GanttPresenter.MyView {

    public interface GanttViewUiBinder extends UiBinder<PopupPanel, GanttView> {
    }

    private GanttChart ganttChart;

    final private PopupPanel ganttPanel;

    @SuppressWarnings("unused")
    private GanttUiHandlers uiHandlers;

    @UiField
    SimplePanel chartPanel;

    @UiField
    Button closeButton;

    final GanttChartListener ganttChartListener = new GanttChartListener() {
	@Override
	public void onTaskItemClick(TaskItem item) {
	    // itemEditPanel.setTask(item);
	}
    };

    @Inject
    public GanttView(final EventBus eventBus, final GanttViewUiBinder ganttViewUiBinder) {
	super(eventBus);
	ganttPanel = ganttViewUiBinder.createAndBindUi(this);
	ganttChart = GanttChart.create();
    }

    @Override
    public void add(TaskItem ganttTask) {
	if (null == ganttChart) {
	    ganttChart = GanttChart.create();
	}
	ganttChart.addTaskItem(ganttTask);
    }

    @Override
    public Widget asWidget() {
	return ganttPanel;
    }

    public void generate() {
	ganttChart.generate();
    }

    @Override
    public void generateSampleContent() {
	if (null == ganttChart) {
	    ganttChart = GanttChart.create();
	}
	ganttChart.addTaskItem(createTaskItem("1", "Define Chart API", (Date) null, (Date) null, Priority.BLACK, "", 0, "Brian", 0, true, "0", 1));
	ganttChart.addTaskItem(createTaskItem("11", "Chart Object", Format.getDate("2/20/2008"), Format.getDate("2/20/2008"), "ff00ff", "http://www.yahoo.com", 1, "Shlomy", 100, false, "1", 1));
	ganttChart.addTaskItem(createTaskItem("12", "Task Objects", (Date) null, (Date) null, "00ff00", "", 0, "Shlomy", 40, true, "1", 1));
	ganttChart.addTaskItem(createTaskItem("121", "Constructor Proc", Format.getDate("2/21/2008"), Format.getDate("3/9/2008"), "00ffff", "http://www.yahoo.com", 0, "Brian T.", 60, false, "12", 1));
	ganttChart.addTaskItem(createTaskItem("122", "Task Variables", Format.getDate("3/6/2008"), Format.getDate("3/11/2008"), Priority.BLACK, "http://help.com", 0, "", 60, false, "12", 1, "121"));
	ganttChart.addTaskItem(createTaskItem("123", "Task Functions", Format.getDate("3/9/2008"), Format.getDate("3/28/2008"), Priority.BLACK, "http://help.com", 0, "Anyone", 10, false, "12", 1));
	ganttChart.addTaskItem(createTaskItem("2", "Create HTML Shell", Format.getDate("3/24/2008"), Format.getDate("3/25/2008"), "ffff00", "http://help.com", 0, "Brian", 20, false, "0", 1, "122"));
	ganttChart.addTaskItem(createTaskItem("3", "Code Javascript", (Date) null, (Date) null, Priority.BLACK, "http://help.com", 0, "Brian", 0, true, "0", 1));
	ganttChart.addTaskItem(createTaskItem("31", "Define Variables", Format.getDate("2/25/2008"), Format.getDate("3/17/2008"), "ff00ff", "http://help.com", 0, "Brian", 30, false, "3", 1));
	ganttChart.addTaskItem(createTaskItem("32", "Calculate Chart Size", Format.getDate("3/15/2008"), Format.getDate("3/24/2008"), "00ff00", "http://help.com", 0, "Shlomy", 40, false, "3", 1));
	ganttChart.addTaskItem(createTaskItem("33", "Draw Taks Items", (Date) null, (Date) null, "00ff00", "http://help.com", 0, "Someone", 40, true, "3", 1));
	ganttChart.addTaskItem(createTaskItem("332", "Task Label Table", Format.getDate("3/6/2008"), Format.getDate("3/11/2008"), Priority.ORANGE, "http://help.com", 0, "Brian", 60, false, "33", 1));
	ganttChart.addTaskItem(createTaskItem("333", "Task Scrolling Grid", Format.getDate("3/9/2008"), Format.getDate("3/29/2008"), Priority.ORANGE, "http://help.com", 0, "Brian", 60, false, "33", 1));
	ganttChart.addTaskItem(createTaskItem("34", "Draw Task Bars", (Date) null, (Date) null, "990000", "http://help.com", 0, "Anybody", 60, true, "3", 1));
	ganttChart.addTaskItem(createTaskItem("341", "Loop each Task", Format.getDate("3/26/2008"), Format.getDate("4/11/2008"), Priority.BLACK, "http://help.com", 0, "Brian", 60, false, "34", 1));
	ganttChart.addTaskItem(createTaskItem("342", "Calculate Start/Stop", Format.getDate("4/12/2008"), Format.getDate("5/18/2008"), "ff6666", "http://help.com", 0, "Brian", 60, false, "34", 1));
	ganttChart.addTaskItem(createTaskItem("343", "Draw Task Div", Format.getDate("5/13/2008"), Format.getDate("5/17/2008"), Priority.BLACK, "http://help.com", 0, "Brian", 60, false, "34", 1));
	ganttChart.addTaskItem(createTaskItem("344", "Draw Completion Div", Format.getDate("5/17/2008"), Format.getDate("6/04/2008"), Priority.BLACK, "http://help.com", 0, "Brian", 60, false, "34", 1));
    }

    @Override
    public void removeFromSlot(Object slot, Widget content) {
    }

    @Override
    public void setUiHandlers(GanttUiHandlers uiHandlers) {
	this.uiHandlers = uiHandlers;
    }

    @Override
    public void show() {
	ganttChart.setGanttChartListener(ganttChartListener);
	generate();
	chartPanel.add(ganttChart);
	center();
	super.show();
    }

    private TaskItem createTaskItem(String pID, String pName, Date pStart, Date pEnd, String pColor, String pLink, int pMile, String pResponsible, int pComp, boolean isGroup, String pParent, int pOpen) {
	// fillList(pID, pName, isGroup);
	return TaskItem.create(pID, pName, pStart, pEnd, pColor, pLink, pMile, pResponsible, pComp, isGroup, pParent, pOpen);
    }

    private TaskItem createTaskItem(String pID, String pName, Date pStart, Date pEnd, String pColor, String pLink, int pMile, String pResponsible, int pComp, boolean isGroup, String pParent, int pOpen, String pDependsOn) {
	// fillList(pID, pName, isGroup);
	return TaskItem.create(pID, pName, pStart, pEnd, pColor, pLink, pMile, pResponsible, pComp, isGroup, pParent, pOpen, pDependsOn);
    }

    @UiHandler("closeButton")
    void okButtonClicked(ClickEvent event) {
	ganttChart = ganttChart.clear(ganttChart);
	ganttPanel.hide();
    }
}
